<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP, "6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');
$redis->auth(REDIS_PASS);

$model = new Model;
$localDir = LIUDOUMI_ROOT_PATH;
$upyun = new UpYun(UPYUN_BUCKET_LDM, UPYUN_USER, UPYUN_PASS);

$limit = 1000;
while($limit--) {
  	$stop = $redis->get('LNPAN_QUEUE_STOP');
  	if($stop) {
    	die('stop');
  	}

  	if($limit <= 1) {
  		sleep(2);
  		$limit = 1000;
  		continue;
  	}

  	$id = $redis->lPop(LIUDOUMI_YUN_QUEUE);
  	//$id =20040;
  	if(!$id) {
  		die('empty queue');
  	}
  	$sql  = "select n.id, n.imgurl, m.content, m.id as content_id
						from yl_news n
						left join yl_content m on n.id = m.news_id
						where n.id = $id   
						order by n.id asc
						limit 1
		";
    $news = $model->query($sql);
  	if(!$news) {
  		die('empty info');
  	}

  	$is_exception = false;
	$news = $news[0];

	//上传封面
	if($news['imgurl'] && stristr($news['imgurl'], 'liudoumi.com')) {
		$news['imgurl'] = str_ireplace('%2F', '/', $news['imgurl']);
		$parse_url = parse_url($news['imgurl']);
		$basename = basename($news['imgurl']);
		$filename = $localDir . $parse_url['path'];

		if(!file_exists($filename) || !stristr($news['imgurl'], 'liudoumi.com')) {
			var_dump($filename, $news['imgurl']);
			continue;
		}

		$upfile = '/thumb/'.$basename;
		try {
			$fh = fopen($filename, 'rb');
			$rsp = $upyun->writeFile($upfile, $fh, True); //上传图片，自动创建目录
			fclose($fh);
			if($rsp) {
			  	$redis->lPush(LIUDOUMI_YUN_UPLOAD_THUMB_SUCCESS, date('Y-m-d H:i:s') . ':' . $news['id'] .':http://dl.liudoumi.com'.$upfile);
				$news['imgurl'] = 'http://dl.liudoumi.com'.$upfile;
			} else {
			  	$redis->lPush(LIUDOUMI_YUN_UPLOAD_THUMB_FAIL, date('Y-m-d H:i:s') . ':' . $news['id'] .':'.$rsp);
				$is_exception = true;
			}
		}
		catch(Exception $e) {
			$error = $e->getCode().':'.$e->getMessage();
			$redis->lPush(LIUDOUMI_YUN_UPLOAD_THUMB_FAIL, date('Y-m-d H:i:s') . ':' . $news['id'] .':'.$error);
			$is_exception = true;
		}
	}

	//上传内容图片
	$allImages = array();
	preg_match_all('/src=[\'|"](.+?)[\'|"]/i', $news['content'], $out);
	foreach ($out[1] as $image) {
		$image = str_ireplace('%2F', '/', $image);
		$parse_url = parse_url($image);
		$basename = basename($image);
		$filename = $localDir . $parse_url['path'];

		if(!file_exists($filename) || !stristr($image, 'liudoumi.com')) {
			var_dump($filename, $image);
			continue;
		}

		$upfile = '/images/'.$basename;
		try {
			$result = $upyun->getFileInfo($upfile);
			if($result) {
				$news['content'] = str_replace($image, 'http://dl.liudoumi.com'.$upfile, $news['content']);
				$allImages[] = $upfile;
				var_dump($result);
				continue;
			}
		}
		catch(Exception $ex) {
			try {
				$fh = fopen($filename, 'rb');
				$rsp = $upyun->writeFile($upfile, $fh, True); //上传图片，自动创建目录
				fclose($fh);
				if($rsp) {
				  	$redis->lPush(LIUDOUMI_YUN_UPLOAD_SUCCESS, date('Y-m-d H:i:s') . ':' . $news['id'] .':http://dl.liudoumi.com'.$upfile);
					$allImages[] = $upfile;
					$news['content'] = str_replace($image, 'http://dl.liudoumi.com'.$upfile, $news['content']);
				} else {
				  	$redis->lPush(LIUDOUMI_YUN_UPLOAD_FAIL, date('Y-m-d H:i:s') . ':' . $news['id'] .':'.$rsp);
					$is_exception = true;
				}
			}
			catch(Exception $e) {
				$error = $e->getCode().':'.$e->getMessage();
				$redis->lPush(LIUDOUMI_YUN_UPLOAD_FAIL, date('Y-m-d H:i:s') . ':' . $news['id'] .':'.$error);
				$is_exception = true;
			}
		}
	}

	if($is_exception) {
		$redis->rPush(LIUDOUMI_YUN_QUEUE, $id);
		var_dump($id.':is_exception');
	}

	if(!$out[1]) {
		$update_data = array();
		$update_data['state'] = 5;
		$update_data['imgurl'] = $news['imgurl'];
		$model->save('yl_news', "id='$news[id]'", $update_data);
		var_dump($id.':image empty');
	}

	if($allImages) {
		$update_data = array();
		$update_data['content'] = $news['content'];
		$model->save('yl_content', "id='$news[content_id]'", $update_data);

		$update_data = array();
		$update_data['state'] = 5;
		$update_data['imgurl'] = $news['imgurl'];
		$model->save('yl_news', "id='$news[id]'", $update_data);

		var_dump($id.':image upload');
	}
}
